﻿--[[
   @brief    话题直播房间control
   @author   rickonzhang
   @version  1.0
   @date     2016-08-08
--]]

live_room_control = 
{
    func_name_to_func_ptr = {};
    is_web_inited = {};
    is_start_flush_ui_timer = false;

    max_session_btn_signaled = false;
    mini_session_btn_signaled = false;

    begin_enter_ = 0;
    open_topic_room_id_ = 0;
};

function live_room_control.log(log_str, log_level)
    local is_true = false;
    if log_level == true then
        is_true = true;
    end
    base_utility.log("[live_room] - control - "..log_str, is_true);
end

function live_room_control.init()
    live_room_control.log("init");
    live_room_control.func_name_to_func_ptr["web_inited"] = live_room_control.web_inited;
    live_room_control.func_name_to_func_ptr["send_msg_to_group"] = live_room_control.send_msg_to_group;
    live_room_control.func_name_to_func_ptr["close_sessions"] = live_room_control.close_sessions;
    live_room_control.func_name_to_func_ptr["get_group_all_message"] = live_room_control.get_group_all_message;
    live_room_control.func_name_to_func_ptr["open_img_browse_floder"] = live_room_control.open_img_browse_floder;
    live_room_control.func_name_to_func_ptr["show_pic"] = live_room_control.show_pic;
    live_room_control.func_name_to_func_ptr["paste_pic"] = live_room_control.paste_pic;
    live_room_control.func_name_to_func_ptr["upload_pic"] = live_room_control.upload_pic;
    live_room_control.func_name_to_func_ptr["set_web_ctrl_focus"] = live_room_control.set_web_ctrl_focus;

    comm_center.reg_service("i_service_social_open_topic_room", live_room_control.service_open_topic_room);
end

function live_room_control.service_open_topic_room(input)
    live_room_control.log("service_open_topic_room: "..base_utility.table_to_json(input));
    if input["room_id"] ~= nil and input["room_id"] ~= 0 then
        if input["game_id"] ~= nil and input["game_id"] ~= 0 then --进游戏话题房间
            comm_center.call_service('i_service_social_open_common_room_talk', input);
        else --进主面板话题房间
            local room_info = live_room_model.get_cur_live_room_info();
            if room_info ~= nil and room_info.room_base_info.room_id == input["room_id"] then
                local match_group_info = group_list_model.get_match_room_info(room_info.room_base_info.room_id); --判断窗口是否打开过了，提高性能的作用。
                if match_group_info ~= nil then
                    live_room_control.click_join_cur_live_room();
                else
                    live_room_control.open_topic_room_id_ = input["room_id"];
                    live_room_model.is_click_join_btn_ = true; --这个标志表示拉取房间信息后就触发进房的逻辑。为下面这一句代码准备的。
                    live_room_control.get_cur_live_room_info();
                end;
            else
                live_room_control.open_topic_room_id_ = input["room_id"];
                live_room_model.is_click_join_btn_ = true; --这个标志表示拉取房间信息后就触发进房的逻辑。为下面这一句代码准备的。
                live_room_control.get_cur_live_room_info();
            end;
            --comm_center.call_service('i_service_social_open_live_room_talk', {group_id = input["room_id"]});

            
        end;
    end;
end;

function live_room_control.on_im_inited(event_id, json_str)
    comm_center.call_service("im_app_service_get_live_room_cfg", {});
end

--{"cfg_list":[{"cfg_name":"flush_time","cfg_value":"600"}],"crc":0,"result":0}
function live_room_control.on_get_live_room_cfg(data)
    live_room_control.log("on_get_live_room_cfg:"..base_utility.table_to_json(data), true);
    if data.result == 200 then
        local param = base_utility.json_to_table(data.result_str);
        if param ~= nil and param.result == 0 then
            for index, item in pairs(param.cfg_list) do
                if item.cfg_name == "flush_time" then
                    local time = tonumber(item.cfg_value);
                    live_room_model.set_ui_update_inter(time);
                    break;
                end
            end
        end
    end
end

function live_room_control.on_js_callback(uilib, fuc_name, json_str, ctrl_id)
    live_room_control.log("on_js_callback ctrl_id: "..tostring(ctrl_id)..", func_name: "..fuc_name..", param: "..tostring(json_str));
    if live_room_control.func_name_to_func_ptr[fuc_name] ~= nil then
        live_room_control.func_name_to_func_ptr[fuc_name](json_str, ctrl_id);
    end
end

function live_room_control.web_inited(json_str, ctrl_id)
    local wnd_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local group_id = live_room_model.get_group_id_from_dlg_id(wnd_id);
    if group_id ~= nil then
        live_room_control.call_web_create_talk_session(group_id, ctrl_id);
        live_room_control.is_web_inited[group_id] = true;
    end
end

function live_room_control.send_msg_to_group(json_str, ctrl_id)
    local param = base_utility.json_to_table(json_str);
    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:send_msg_to_group(param.group_id, param.msg);
end

function live_room_control.close_sessions(json_str, ctrl_id)
    local dlg_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    live_room_view.close_wnd(dlg_id)
end

function live_room_control.get_group_all_message(json_str, ctrl_id)
    local param = base_utility.json_to_table(json_str);
    group_id = param.group_id;

    local history_unread_msg_count = group_list_model.get_group_history_unread_msg_count_remotely(group_id);
    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    
    if group_list_model.get_has_ever_got_msg(group_id) == false then
        local  msg_count = 60;
        if history_unread_msg_count > 60 then
            msg_count = history_unread_msg_count;
        end
        social_network:get_group_msgs(group_id, msg_count); 
    else
        local messages = group_message_model.get_group_messages(group_id);
        local param_table = {};
        
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["group_id"] = group_id;
        param_table["data"] = messages;
        web_utility.lua_call_js_by_id("get_group_all_message",base_utility.table_to_json(param_table), ctrl_id);
        base_utility.log(string.format("[talk_dialog_control]on_get_group_all_message, str = %s",base_utility.table_to_json(param_table)));
        talk_dialog_control.save_history_msg(group_id, messages);
    end

    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:set_msg_read(talk_dialog_control.GROUP_FLAG, group_id);

    group_list_model.clear_history_unread_msg_count(group_id); 
    group_list_model.clear_history_unread_msg_count_remotely(group_id);
    group_message_model.clear_group_messages(group_id);

    local table_param = {};
    table_param["group_id"] = group_id;
    event_center.send_event(group_event.GROUP_CLEAR_MESSAGE,base_utility.table_to_json(table_param));
end

function live_room_control.on_get_group_unread_message(group_id, messages_table)
    local web_ctrl_id = live_room_model.get_web_ctrl_id_by_group_id(group_id)
    if web_ctrl_id > 0 then
        web_utility.lua_call_js_by_id("get_group_all_message", base_utility.table_to_json(messages_table), web_ctrl_id);
    end
end

function live_room_control.on_recv_group_report(group_id, group_report_tab)
    local web_ctrl_id = live_room_model.get_web_ctrl_id_by_group_id(group_id)
    if web_ctrl_id > 0 then
        web_utility.lua_call_js_by_id("recv_group_report", base_utility.table_to_json(group_report_tab), web_ctrl_id);
        if group_report_tab.data.group_report.type == 0x04 then -- 退群
            local dlg_id = live_room_model.get_dlg_id_from_group_id(group_id)
            if dlg_id > 0 then
                live_room_view.close_wnd(dlg_id, true);
            end
        end
    end
    local g_report = group_report_tab.data.group_report;
    if g_report.type == 0xFF then
        local param = base_utility.json_to_table(g_report.remark);
        -- http://tapd.oa.com/mtgp_server/markdown_wikis/view/#1010119991005523227
        if param.type == 1014 then --话题热度
            live_room_control.log("on_recv_group_report update live room count:"..tostring(param.user_num));
            live_room_model.update_cur_live_room_number(tonumber(param.user_num));
            live_room_view.update_live_room_info_ui();
        elseif param.type == 1011 or param.type == 1010 then --文字直播普通消息、直播上墙消息
            --构造一条消息，更新到最近消息 
            live_room_control.make_msg_to_last_msg(g_report.group_id);
        end
    end
end

function live_room_control.make_msg_to_last_msg(group_id)
    -- bug fix:http://tapd.oa.com/10041181/bugtrace/bugs/view?bug_id=1010041181056430455
    -- 看日志 其实登录已经成功了。但是这里使用login_model.im_login_infomation 居然为nil 
    -- 暂时 增加代码保护
    if login_model.im_login_infomation == nil then
        return;
    end

    local msg = {}
    msg.message = "{\"type\":0,\"text\":\"[" .. LoadStr("SOCIAL_TALK_DIALOG_PIC_TEXT_LIVE_MESSAGE") .. "]\"}"
    msg.sender = login_model.im_login_infomation.friend_id;
    msg.time = os.time();
    msg.msg_type = 0;
    msg.result = 0;
    msg.time_custom = "0:0";
    msg.action_type = 0;
    msg.group_id = group_id;
    group_list_model.set_last_msg(group_id, msg);

    -- 聊天窗口不可见
    local dlg_id = live_room_model.get_dlg_id_from_group_id(group_id);
    if dlg_id ~= nil then
        local ptr = ui_utility.get_ctrl_ptr_by_id(dlg_id);
        if ptr ~= nil then
            if ptr:IsVisible() == FALSE then
                live_room_control.check_need_tips_live_msg(group_id, 1);
            end
        end
    else
        live_room_control.check_need_tips_live_msg(group_id, 1);
    end

    --消息列表是否可见
    -- local is_max_social_
    -- local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    -- if data.is_show == 1 then --mini mode
    --     live_room_model.increase_unread_live_msg(group_id, 1);
    --     return;
    -- end

    --小界面面板消息列表不见
                                -- 小编上墙消息未读数 +1

    --点击消息面板
    --显示大界面社交界面     -- 清除小编上墙消息未读数
    --打开聊天窗口
end

--检查是否需要提示直播中的小编消息-------------------------------------------------
function live_room_control.check_need_tips_live_msg(group_id, count)
    local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
    if out.mode == 0 then -- max mode
        live_room_control.check_max_mode_live_msg(group_id, count);
    else
        live_room_control.check_mini_mode_live_msg(group_id, count)
    end
end

function live_room_control.check_max_mode_live_msg(group_id, count)
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    -- 消息面板是否不可见
    if data.is_show == 0 or live_room_control.max_session_btn_signaled == false then
        live_room_model.increase_unread_live_msg(group_id, count);
        event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json({show = "true"}));
        comm_center.call_service('i_service_social_network_update_tips_msg_info', {});
    else
        -- 不需要提示
    end
end

function live_room_control.check_mini_mode_live_msg(group_id, count)
    if live_room_control.mini_session_btn_signaled == false then
        live_room_model.increase_unread_live_msg(group_id, count);
        event_center.send_event(group_event.SHOW_FRIEND_TAB_RED_POINT, base_utility.table_to_json({show = "true"}));
        comm_center.call_service('i_service_social_network_update_tips_msg_info', {});
    else
        -- 不需要提示
    end    
end

function live_room_control.get_unread_live_msg_count(data)
    local out = {};
    local count = live_room_model.get_unread_live_msg(data.group_id);
    out.count = count;
    return out;
end
--------------------------------------------------------------------------------------------------

function live_room_control.on_new_group_message_come(group_id, message_tab)
    local web_ctrl_id = live_room_model.get_web_ctrl_id_by_group_id(group_id)
    if web_ctrl_id > 0 then
        web_utility.lua_call_js_by_id("new_group_message_come", base_utility.table_to_json(message_tab), web_ctrl_id);
    end
end

function live_room_control.on_broadcast_query_user_ungoted_msg(tbl_param)
    local web_ctrl_id = live_room_model.get_web_ctrl_id_by_group_id(tbl_param.group_id);
    if web_ctrl_id > 0 then
        local param_table = {};
        param_table["retCode"] = 0;
        param_table["msg"] = "success";
        param_table["data"] = {};
        param_table["data"]["group_id"] = tbl_param.group_id;
        local count = group_list_model.get_group_history_unread_msg_count(tbl_param.group_id);
        param_table["data"]["count"] = count;
        web_utility.lua_call_js_by_id("new_group_message_come", base_utility.table_to_json(param_table), web_ctrl_id);
    end
end

--监听群列表获取成功后拉取话题房间入口
function live_room_control.get_group_list_done(event_id, json_str)
    --live_room_control.get_cur_live_room_info()
end

function live_room_control.get_cur_live_room_info()
    comm_center.call_service('im_app_service_get_cur_live_room_info', {tgp_id = login_model.im_login_infomation.friend_id});
end

-- 获取话题房间信息结果
-- 返回结果参考：http://tapd.oa.com/TGC/markdown_wikis/view/#1010041181005412879 拉取当前正在开放的直播房间
function live_room_control.on_get_cur_live_room_info(data)
    live_room_control.log("on_get_cur_live_room_info:"..base_utility.table_to_json(data));
    if data.result == 200 then -- http 请求结果
        local param = base_utility.json_to_table(data.result_str);
        if type(param) == "table" then
            if param.ErrorCode == 0 then -- 社交后台请求qt的结果
                local room_info = param.Data;
                if room_info.result == 0 then --qt后台业务层结果
                    local open_topic_room_id_ = live_room_control.open_topic_room_id_;
                    if live_room_control.open_topic_room_id_ ~= 0 then
                        local open_topic_room_id = live_room_control.open_topic_room_id_;
                        live_room_control.open_topic_room_id_ = 0;
                        -- 以下是做service入口调用进房的错误处理：可能room_id失效了或者其他情况。
                        if room_info.room_base_info == nil then
                            live_room_control.log("on_get_cur_live_room_info invalid id, dest:"..tostring(open_topic_room_id)
                                ..",current: nil");
                            live_room_model.is_click_join_btn_ = false;
                            live_room_control.tips_error(LoadStr("SOCIAL_TALK_DIALOG_ROOM_OVERTIME"));
                            return;
                        end;
                        if room_info.room_base_info ~= nil and open_topic_room_id ~= room_info.room_base_info.room_id then
                            live_room_control.log("on_get_cur_live_room_info invalid id, dest:"..tostring(open_topic_room_id)
                                ..",current:"..tostring(room_info.room_base_info.room_id));
                            live_room_model.is_click_join_btn_ = false;
                            live_room_control.tips_error(LoadStr("SOCIAL_TALK_DIALOG_ROOM_OVERTIME"));
                            return;
                        end;
                    end;
                    --直播入口关闭
                    if room_info.cur_match_flag == 0 then                        
                        -- 判断是否已经加入
                        -- 如果已经加入，则入口保持不变
                        -- 否则，关闭入口
                        local match_group_info = nil;
                        if room_info.room_base_info ~= nil then
                           match_group_info = group_list_model.get_match_room_info(room_info.room_base_info.room_id);
                        end
                        if match_group_info == nil then
                            live_room_model.is_live_room_closed_ = true;
                            live_room_view.update_live_room_info_ui();
                        end
                        return;
                    end              

                    live_room_model.is_live_room_closed_ = false;

                    local old_live_room_info = live_room_model.get_cur_live_room_info();
                    if old_live_room_info ~= nil and 
                       live_room_model.is_me_in_match_live_room_group(old_live_room_info.room_base_info.room_id) then
                        -- 更新请求的返回
                        -- 检测话题房间id是否变更(挂机用户)
                        if old_live_room_info == nil or 
                            (old_live_room_info.room_base_info.room_id == room_info.room_base_info.room_id) then
                            live_room_model.set_cur_live_room_info(room_info);
                            live_room_view.update_live_room_info_ui();
                        else
                            -- no need update
                        end
                    else
                        live_room_model.set_cur_live_room_info(room_info);
                        live_room_view.update_live_room_info_ui();      

                        --自己没有加入话题房间 如果此时话题房间信息更新了，需要重新提示
                        if old_live_room_info ~= nil and 
                            (old_live_room_info.room_base_info.room_id ~= room_info.room_base_info.room_id) then
                            live_room_control.update_social_web_title_tips();
                        end           
                    end

                    if open_topic_room_id_ ~= 0 then -- 飞机票打开的

                        if live_room_model.is_click_join_btn_ == true then
                            live_room_model.is_click_join_btn_ = false;

                            --删除老的群组信息
                            if old_live_room_info ~= nil and old_live_room_info.room_base_info ~= nil then
                                local match_group_info = group_list_model.get_match_room_info(old_live_room_info.room_base_info.room_id);
                                if match_group_info ~= nil then
                                    local dlg_id = live_room_model.get_dlg_id_from_group_id(match_group_info.group_id)
                                    if dlg_id > 0 then
                                        live_room_view.close_wnd(dlg_id, true);
                                        group_list_view.remove_group_adapter(match_group_info.group_id);
                                    end
                                end
                            end;

                            live_room_model.set_cur_live_room_info(room_info); --更新主面板
                            live_room_view.update_live_room_info_ui(); --更新主面板
                            live_room_control.join_cur_live_room();
                        end
                    else -- 不是飞机票打开的
                        if live_room_model.is_click_join_btn_ == true then
                            live_room_model.is_click_join_btn_ = false;
                            live_room_control.join_cur_live_room();
                        end
                    end;



                    if  old_live_room_info == nil then
                        --首次获返回，启动1s定时器，用来检测入口 ui是否可见
                        local tpfui =
                            base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");
                        tpfui:AddInterval('live_room_control.timer_1000ms_for_check', 1000);

                        live_room_view.update_live_room_info_ui();
                        live_room_control.update_social_web_title_tips();
                    end
                else
                    live_room_control.log("on_get_cur_live_room_info error, qt result:"..tostring(room_info.result));

                    if live_room_control.open_topic_room_id_ ~= 0 then
                        local open_topic_room_id = live_room_control.open_topic_room_id_;
                        live_room_control.open_topic_room_id_ = 0;
                        -- 以下是做service入口调用进房的错误处理：可能room_id失效了或者其他情况。

                        live_room_control.log("on_get_cur_live_room_info got errorcode, dest:"..tostring(open_topic_room_id));
                        live_room_model.is_click_join_btn_ = false;
                        live_room_control.tips_error(LoadStr("SOCIAL_TALK_DIALOG_ROOM_OVERTIME"));
                    end;
                end
            else
                live_room_control.log("on_get_cur_live_room_info error, ErrorCode:"..tostring(param.ErrorCode));
            end
        else
            live_room_control.log("on_get_cur_live_room_info error, str:"..tostring(data.result_str));            
        end
    else
        live_room_control.log("on_get_cur_live_room_info error, result:"..tostring(data.result));
    end
end

function live_room_control.show_room_dlg(group_id)
    live_room_view.show(group_id);
    live_room_model.clear_unread_live_msg(group_id);
    if live_room_control.is_web_inited[group_id] == true then
        local web_ctrl_id = live_room_model.get_web_ctrl_id_by_group_id(group_id);
        live_room_control.call_web_create_talk_session(group_id, web_ctrl_id)
    end
end

function live_room_control.call_web_create_talk_session(group_id, web_ctrl_id)
    local param_table = {};
    param_table["retCode"] = 0;
    param_table["msg"] = "success";
    param_table["data"] = {};
    param_table["data"]["room_info"] = {};

    local group_info = group_list_model.get_group_info(group_id);
    local ext_info = base_utility.json_to_table(group_info.ext_data2);
    if ext_info ~= nil then

        param_table["data"]["room_info"]["group_id"] = group_id;
        param_table["data"]["type"] = "MatchLiveRoom";
        if group_info.group_type == "CommonTopicRoom" then
            param_table["data"]["room_info"]["game_id"] = ext_info.game;
            param_table["data"]["type"] = "CommonTopicRoom";
        elseif group_info.group_type == "MatchLiveRoom" then
            param_table["data"]["room_info"]["member_cnt"] = 0;
            local room_info = live_room_model.get_cur_live_room_info();
            if room_info ~= nil then
                param_table["data"]["room_info"]["member_cnt"] = room_info.cur_match_info.member_cnt;
            end
        end
        param_table["data"]["room_id"] = ext_info.room_id;

        live_room_control.log("call_web_create_talk_session doing, group_info:"..base_utility.table_to_json(group_info));
        web_utility.lua_call_js_by_id("create_talk_session", base_utility.table_to_json(param_table), web_ctrl_id);
    else
        live_room_control.log("call_web_create_talk_session error, group_info:"..base_utility.table_to_json(group_info));
    end
end

function live_room_control.click_join_cur_live_room()
    -- 判断是否已经加入
    local room_info = live_room_model.get_cur_live_room_info();
    if room_info == nil then
        return;
    end
    local match_group_info = group_list_model.get_match_room_info(room_info.room_base_info.room_id);
    if match_group_info ~= nil then
        live_room_control.show_room_dlg(match_group_info.group_id);
    else
        live_room_model.is_click_join_btn_ = true;
        --点击时先查询一次信息
        live_room_control.get_cur_live_room_info();
    end
end

function live_room_control.join_cur_live_room()
    local room_info = live_room_model.get_cur_live_room_info();
    if room_info ~= nil then
        -- 没有加入，则调用后台先进群
        local param = {tgp_id = login_model.im_login_infomation.friend_id,
                       room_id = tostring(room_info.room_base_info.room_id),
                       room_type = 1};
        comm_center.call_service('im_app_service_join_cur_live_room', param);
    end
end

function live_room_control.on_join_cur_live_room(data)
    live_room_control.log("on_join_cur_live_room:"..base_utility.table_to_json(data), true);
    if data.result == 200 then
        local param = base_utility.json_to_table(data.result_str);
        local data = param.Data;
        if data ~= nil and data.result == 0 then
            if string.find(data.group_id, "COMM_TOPIC") ~= nil then
                -- 通用游戏话题房间
                -- 目前不需要做什么
            else
                -- 直播话题房间
                live_room_control.cur_live_room_group_id = data.group_id;
                live_room_model.save_room_id_new_flag_showed(data.room_id);   
                live_room_view.update_live_room_info_ui();
            end
      
            --为了避免im推送拉进群通知丢失，这里做兼容处理
            --发起请求群信息
            live_room_control.begin_enter_ = data.group_id; --进群标志
            live_room_control.get_group_info(live_room_control.begin_enter_);
        else
            local errCode = 1;
            if data ~= nli and data.result ~= nil then
                errCode = data.result;
            end
            live_room_control.tips_join_error(errCode);
        end
    end
end

function live_room_control.tips_join_error(errCode)
    local descText = "";
    if 1001 == errCode then
        descText = LoadStr("SOCIAL_TALK_DIALOG_CANNOT_JOIN_ROOM");
    else
        descText = LoadStr("SOCIAL_TALK_DIALOG_CANNOT_JOIN_RETRY");
    end
    live_room_control.tips_error(descText);
end

function live_room_control.tips_error(descText)
    local param = {
                position = 'center',--参数值: rbottom-右下角，center-居中
                title = LoadStr("SOCIAL_TALK_DIALOG_TIPS"),
                width = 480,
                height = 230,
                titleIcon = "",--参数值: ONEIMAGE:login\\base\\2\\login_bg.jpg
                icon = "",
                tipsIcon = "",
                descTitle = "",
                desc = descText,
                cancelBtn = {
                    visible = false,
                    text = LoadStr("SOCIAL_TALK_DIALOG_CANCEL"),
                    handle = function()
                    end
                },
                okBtn = {
                    visible = true;
                    text = LoadStr("SOCIAL_TALK_DIALOG_CONFIRM"),
                    handle = function()
                        poptips_control.close_tips();
                    end
                }
    };
    poptips_control.show_tips(param);
end

function live_room_control.get_group_info(group_id)
    live_room_control.is_send_get_group_info_req = true;
    local group_list = {};
    group_list["id_list"] = {};
    table.insert(group_list["id_list"], group_id);
    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:get_group_info(base_utility.table_to_json(group_list)); 
end

function live_room_control.on_get_group_info(event_id, json_str)
    if live_room_control.is_send_get_group_info_req == true then
        local param = base_utility.json_to_table(json_str);
        if param.result == 0 and param.count == 1 then
            local g_info = param.groups[1];
            if g_info ~= nil and g_info.group_id == live_room_control.begin_enter_ then
                live_room_control.log("on_get_group_info group_id:"..tostring(g_info.group_id), true);
                live_room_control.is_send_get_group_info_req = nil;
                local now_g_info = group_list_model.get_group_info(live_room_control.begin_enter_);
                if now_g_info ~= nil then
                    --已经收到过进群通知了
                    live_room_control.log("already join success", true);
                else
                    live_room_control.log("not already join success", true);
                    --发送事件，转到群列表更新
                    event_center.send_event(social_event.LIVE_ROOM_INVITED_IN_GROUP, base_utility.table_to_json(g_info));                    
                end
                if live_room_control.begin_enter_ == g_info.group_id then
                    live_room_control.begin_enter_ = 0; --进群标志
                    comm_center.call_service('i_service_social_open_live_room_talk', {group_id = g_info.group_id});
                end;
            end
        end 
    end
end

function live_room_control.on_add_group_invited(event_id, json_str)
    live_room_control.log("on_add_group_invited");
    local group_info = base_utility.json_to_table(json_str);
    if group_info.group_type == "MatchLiveRoom" then
        live_room_control.cur_live_room_group_id = group_info.group_id;
        live_room_control.log("on_add_group_invited MatchLiveRoom");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval("live_room_control.process_on_add_group_invited", 1);
    end        
end

function live_room_control.process_on_add_group_invited(ts)
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval("live_room_control.process_on_add_group_invited");
    comm_center.call_service('i_service_social_open_live_room_talk', {group_id = live_room_control.cur_live_room_group_id});
end

function live_room_control.exit_live_room_group(data)
    
end

function live_room_control.on_exit_live_room(data)
    live_room_control.log("on_exit_live_room: "..base_utility.table_to_json(data), true);
    if data.result == 200 then
        local param = base_utility.json_to_table(data.result_str);
        if param.ErrorCode == 0 and
            param.Data ~= nil and
            param.Data.result == 0 then
            comm_center.call_service('i_service_increase_click_on_exit_live_and_success_room_cnt', {});
       else
            comm_center.call_service('i_service_increase_click_on_exit_live_and_failure_room_cnt', {});
       end
    else
        comm_center.call_service('i_service_increase_click_on_exit_live_and_failure_room_cnt', {});
    end
end

function live_room_control.open_live_room_talk(data)
    local group_info = group_list_model.get_group_info(data.group_id);
    if group_info ~= nil then
        if group_info.group_type == "MatchLiveRoom" or 
           group_info.group_type == "CommonTopicRoom"  then
            live_room_control.show_room_dlg(group_info.group_id)
        end
    else
    end
end

function live_room_control.open_img_browse_floder(json_str, ctrl_id)
    local table_param = base_utility.json_to_table(json_str);
    local dlg_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    live_room_view.set_focus_dialog(dlg_id);
    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:open_img_browse_floder(live_room_view.get_dialog_hwnd(dlg_id), table_param.id, table_param.no);
end

function live_room_control.on_open_browse_img(group_id, data_table)
    local web_ctrl_id = live_room_model.get_web_ctrl_id_by_group_id(group_id)
    if web_ctrl_id > 0 then
        web_utility.lua_call_js_by_id("open_img_browse_floder", base_utility.table_to_json(data_table), web_ctrl_id);
    end
end

function live_room_control.show_pic(json_str, ctrl_id)
    local table_param = base_utility.json_to_table(json_str);
    local dlg_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:show_pic(live_room_view.get_dialog_hwnd(dlg_id), table_param.url, table_param.file);
end

function live_room_control.paste_pic(json_str, ctrl_id)
    local table_param = base_utility.json_to_table(json_str);
    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:paste_pic(table_param.id, table_param.no);
end

function live_room_control.upload_pic(json_str, ctrl_id)
    local table_param = base_utility.json_to_table(json_str);
    local social_network =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network:upload_pic(table_param.file_path, table_param.md5, table_param.type);
end

function live_room_control.set_web_ctrl_focus(json_str, ctrl_id)
    local web_ptr = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
    if web_ptr ~= nil then
        web_ptr:SetFocus();
    end
end

function live_room_control.on_tgp_st_renewed(for_web_info)
    for k, dlg_id in pairs(live_room_model.group_id_to_dlg_id_) do
        local web_ctrl_id = ui_utility.get_ctrl_id_by_path(dlg_id, "QBWebBrowser_Talk");
        if web_ctrl_id > 0 then
            web_utility.lua_call_js_by_id("set_social_cookie", base_utility.table_to_json(for_web_info), web_ctrl_id);
        end
    end
end

function live_room_control.update_social_web_title_tips()
    if live_room_model.is_room_id_new_flag_showed() == false then
        event_center.send_event(group_event.SHOW_GROUP_TAB_RED_POINT, base_utility.table_to_json({show = "true"}));
    end

    --检查新话题房间是否需要在web title提示
    if live_room_view.is_room_entry_visible() then
        --ui可见的情况下 直接保存为已经显示过了tips了
        local room_info = live_room_model.get_cur_live_room_info();
        live_room_control.write_web_title_tips_id_flag(room_info.room_base_info.room_id);
    end

    comm_center.call_service('i_service_social_network_update_tips_msg_info', {});
end

function live_room_control.on_session_btn_signaled(data)
    local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
    if out.mode == 0 then -- max mode
        live_room_control.max_session_btn_signaled = true;
    else
        live_room_control.mini_session_btn_signaled = true;
    end

    live_room_model.clear_all_unread_live_msg();
    comm_center.call_service('i_service_social_network_update_tips_msg_info', {});
end

function live_room_control.on_session_btn_unsignaled(data)
    local out = comm_center.call_service("i_service_mainframe_get_current_mode", {});
    if out.mode == 0 then -- max mode
        live_room_control.max_session_btn_signaled = false;
    else
        live_room_control.mini_session_btn_signaled = false;
    end
end

function live_room_control.on_contact_btn_unsignaled(data)
    live_room_control.contact_btn_signaled = false;
end

function live_room_control.on_contact_btn_signaled(data)
    live_room_control.contact_btn_signaled = true;
    local room_info = live_room_model.get_cur_live_room_info();
    if room_info ~= nil then
        --保存配置
        live_room_control.write_web_title_tips_id_flag(room_info.room_base_info.room_id);
    end
end

function live_room_control.need_show_tips_on_web_title(data)
    local out = {};
    local room_info = live_room_model.get_cur_live_room_info();
    if room_info == nil then
        out.need = 0;
        return out;
    end

    if live_room_control.read_web_title_tips_id_flag(room_info.room_base_info.room_id) then
        out.need = 0;
    else
        out.need = 1; 
    end
    return out;
end

--保存配置
function live_room_control.write_web_title_tips_id_flag(room_id)
    local out = comm_center.call_service("im_app_service_read_common_cfg", {is_self=1, key="social_new_live_room_web_tips_ids"});
    local id_list = base_utility.json_to_table(out.value);
    for key, value in pairs(id_list) do
        if value.room_id == room_id then
            live_room_control.log("update_social_web_title_tips id is exist:"..tostring(room_id));
            return;
        end
    end
    local one_item = {};
    one_item.room_id = room_id;
    one_item.time = os.time();
    table.insert(id_list, one_item);
    comm_center.call_service("im_app_service_write_common_cfg", {is_self=1, key="social_new_live_room_web_tips_ids", value=base_utility.table_to_json(id_list)});
end

function live_room_control.read_web_title_tips_id_flag(room_id)
    local out = comm_center.call_service("im_app_service_read_common_cfg", {is_self=1, key="social_new_live_room_web_tips_ids"});
    local id_list = base_utility.json_to_table(out.value);
    for key, value in pairs(id_list) do
        if value.room_id == room_id then
            return true;
        end
    end
    return false;
end

----------------------话题房间入口房间信息刷新逻辑----------------------
function live_room_control.timer_1000ms_for_check(ts)
    live_room_control.check_ui_state();
end

function live_room_control.check_ui_state()
    --主窗口可见，入口可见信息显示时才刷新
    if social_utility.is_main_wnd_minisize() == false and 
        live_room_view.is_room_entry_visible() == true then
        live_room_control.start_flush_ui_timer();
    else
        live_room_control.stop_flush_ui_timer();
    end
end

function live_room_control.start_flush_ui_timer()
    if live_room_control.is_start_flush_ui_timer == false then
        live_room_control.log("start_flush_ui_timer");
        local inter = live_room_model.get_ui_update_inter() * 1000;
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:AddInterval('live_room_control.process_flush_ui_timer', inter);
        live_room_control.is_start_flush_ui_timer = true;

        --立即先执行一次查询
        live_room_control.process_flush_ui_timer();
    end
end

function live_room_control.stop_flush_ui_timer()
    if live_room_control.is_start_flush_ui_timer == true then
        live_room_control.log("stop_flush_ui_timer");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('live_room_control.process_flush_ui_timer');
        live_room_control.is_start_flush_ui_timer = false;
    end
end

function live_room_control.process_flush_ui_timer(ms)
    -- live_room_control.log("process_flush_ui_timer");
    live_room_control.get_cur_live_room_info();
end

function live_room_control.get_new_live_room_cnt(data)
    local out = {};
    if live_room_model.get_cur_live_room_info() ~= nil then
        if live_room_model.is_room_id_new_flag_showed() then
            out.count = 0;
        else
            out.count = 1;
        end
    else
        out.count = 0;
    end
    return out;
end

--------------------通用话题房间相关接口和逻辑 begin------------------------
function live_room_control.open_common_room_talk(data_in)
    local g_info = group_list_model.get_common_room_group_info(data_in.room_id);
    if g_info ~= nil then
        --存在话题房间
        local data = {group_id = g_info.group_id};
        live_room_control.open_live_room_talk(data);
    else
        --不存在则加入
        live_room_control.join_game_room(data_in.room_id);
    end
end

function live_room_control.join_game_room(room_id)
    local param = {tgp_id = login_model.im_login_infomation.friend_id,
                   room_id = room_id,
                   room_type = 3};
    comm_center.call_service('im_app_service_join_cur_live_room', param);
end

--------------------通用话题房间相关接口和逻辑 end------------------------
live_room_control.init();